package com.first.teacher.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.first.common.pojo.*;
import com.first.teacher.mapper.*;
import com.first.teacher.service.SubjectService;
import com.first.teacher.vo.ChangeStatusVo;
import com.first.teacher.vo.FIndTeacherCourseVo;
import com.first.teacher.vo.FindCourseVo;
import com.first.teacher.vo.UpdateCourseSubjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @program: finrsteducation-parent
 * @description:
 * @author: 张玉森
 * @create: 2023-06-02 20:02
 **/
@Service
public class SubjectServiceImpl implements SubjectService {
    @Autowired
    private EduSubjectMapper eduSubjectMapper;
    @Autowired
    private EduCourseMapper eduCourseMapper;
    @Autowired
    private CourseDescriptionMapper courseDescriptionMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private TbCheckCourseMapper checkCourseMapper;

    @Override
    public List<EduSubject> getAllMenu() {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", 0);
        wrapper.eq("status_id", 1);
        return eduSubjectMapper.selectList(wrapper);
    }


    @Override
    public Page<EduCourse> fontfindCourse(Integer page, Integer pageSize, FindCourseVo findCourseVo) {
        Page<EduCourse> pages = new Page<>(page, pageSize);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("status", "Normal");
        if (findCourseVo != null) {
            if (findCourseVo.getIsFee() != null && !Objects.equals(findCourseVo.getIsFee(), "")) {
                if (findCourseVo.getIsFee().equals("0")) {
                    wrapper.eq("price", 0);
                }
                if (findCourseVo.getIsFee().equals("2")) {
                    wrapper.gt("price", 0);
                }
            }
            if (findCourseVo.getOneSubjectId() != null && !Objects.equals(findCourseVo.getOneSubjectId(), "")) {
                wrapper.eq("subject_parent_id", findCourseVo.getOneSubjectId());
            }
            if (findCourseVo.getTwoSubjectId() != null && !findCourseVo.getTwoSubjectId().equals("")) {
                wrapper.eq("subject_id", findCourseVo.getTwoSubjectId());
            }
        }
        return eduCourseMapper.selectPage(pages, wrapper);
    }

    @Override
    public Page<EduCourse> findCourseByTeacherId(Integer page, Integer pageSize, FIndTeacherCourseVo teacherCourseVo) {
        Page<EduCourse> page1 = new Page<>(page, pageSize);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if (teacherCourseVo.getCourseName() != "" && teacherCourseVo.getCourseName() != null) {
            wrapper.like("title", teacherCourseVo.getCourseName());
        }
        if (teacherCourseVo.getTeacherId() != "" && teacherCourseVo.getTeacherId() != null) {
            wrapper.eq("teacher_id", teacherCourseVo.getTeacherId());
        }
        return eduCourseMapper.selectPage(page1, wrapper);
    }

    @Override
    public Page<EduCourse> getCoursByTeacherPhone(Integer page, Integer pageSize, String phone, Integer checkStatus) {
        Page<EduCourse> page1 = new Page<>(page, pageSize);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", phone);
        if (checkStatus != 8) { //审核状态
            wrapper.eq("tb.check_status", checkStatus);
        }
        return eduCourseMapper.selectmobilePage(page1, wrapper);
    }

    @Override
    public Object findPidByid(String pid) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("id", pid);
        EduSubject eduSubject = eduSubjectMapper.selectOne(wrapper);
        if (eduSubject.getParentId().equals("0")) {
            return 0;
        }
        return eduSubject;
    }

    @Override
    public List<EduSubject> findsubjectByStatus(Integer page, Integer pageSize) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("status_id", 1);
        List<EduSubject> list = eduSubjectMapper.selectList(wrapper);
        List<EduSubject> permissions = new ArrayList<>();
        //1.所有一级菜单
        for (EduSubject eduSubject : list) {
            if (eduSubject.getParentId().equals("0")) {
                permissions.add(eduSubject);
            }
        }
        //1.所有子菜单
        for (EduSubject eduSubject : permissions) {
            List<EduSubject> arrayList = this.getChildren(eduSubject.getId(), list);
            eduSubject.setChildren(arrayList);
        }
        return permissions;
    }

    @Override
    public Integer changeStatus(ChangeStatusVo changeStatusVo) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(changeStatusVo.getId());
        eduCourse.setStatus(changeStatusVo.getStatus());
        eduCourse.setGmtModified(LocalDateTime.now());
        return eduCourseMapper.updateById(eduCourse);
    }

    @Override
    public Object updateCourseSubject(UpdateCourseSubjectVo updateCourseSubjectVo) {
        EduCourse eduCourse = new EduCourse();

        eduCourse.setSubjectId(updateCourseSubjectVo.getCategoryId2());
        eduCourse.setSubjectParentId(updateCourseSubjectVo.getCategoryId1());
        eduCourse.setTitle(updateCourseSubjectVo.getTitle());
        eduCourse.setPrice(updateCourseSubjectVo.getPrice());
        eduCourse.setCover(updateCourseSubjectVo.getCover());
        eduCourse.setGmtModified(LocalDateTime.now());

        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setGmtModified(LocalDateTime.now());
        courseDescription.setDescription(updateCourseSubjectVo.getDescription());

        if (updateCourseSubjectVo.getId()=="" || updateCourseSubjectVo.getId()==null) {
            QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
            wrapper.eq("mobile",updateCourseSubjectVo.getMobile());
            EduTeacher eduTeacher = teacherMapper.selectOne(wrapper);
            String id = IdUtil.getSnowflakeNextId() + "";
            eduCourse.setGmtCreate(LocalDateTime.now());
            courseDescription.setGmtCreate(LocalDateTime.now());
            eduCourse.setId(id);
            eduCourse.setTeacherId(eduTeacher.getId());
            courseDescription.setId(id);
            TbCheckCourse checkCourse = new TbCheckCourse();
            checkCourse.setCourseId(id);
            checkCourse.setCheckTime(LocalDateTime.now());
            checkCourse.setCheckStatus("2");
            checkCourseMapper.insert(checkCourse);
            eduCourseMapper.insert(eduCourse);
            courseDescriptionMapper.insert(courseDescription);
            return id;
        } else {
            eduCourse.setId(updateCourseSubjectVo.getId());
            courseDescription.setId(updateCourseSubjectVo.getId());
            eduCourseMapper.updateById(eduCourse);
            return courseDescriptionMapper.updateById(courseDescription);
        }

    }

    private List<EduSubject> getChildren(String id, List<EduSubject> list) {
        List<EduSubject> children = new ArrayList<>();
        for (EduSubject eduSubject : list) {
            if (eduSubject.getParentId().equals(id)) {
                children.add(eduSubject);
            }
        }
        return children;
    }

}
